package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcTest1 {

    public static void main(String[] args) throws Exception {
        //jdbc使用
        //1、通过反射加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2、建立连接
        Connection connection = DriverManager.
                getConnection("jdbc:mysql://master:3306/student",
                "root",
                "123456");

        //3、创建执行器
        Statement statement = connection.createStatement();
        String sql="select * from student";

        //4、执行sql语句
        ResultSet rs = statement.executeQuery(sql);

        //5、获取结果

        while (rs.next()){
//            System.out.println(rs.getInt(1));
//            System.out.println(rs.getString(2));
//            System.out.println(rs.getInt(3));
//            System.out.println(rs.getString(4));
            System.out.println(rs.getInt("id"));
            System.out.println(rs.getString("name"));
            System.out.println(rs.getInt("age"));
            System.out.println(rs.getString("sex"));
        }

        //6、关闭、释放资源
        rs.close();
        statement.close();
        connection.close();
    }

}
